package com.ruoyi.common.svgutils;

import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class SVGPathExtractor {

    public static void main(String[] args) throws Exception {
        String svgContent = "";
        // 创建DOM解析器
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new File("D:\\code\\track\\trackprod\\ruoyi-common\\src\\main\\java\\com\\ruoyi\\common\\svgutils\\呼西整备场站场图原始.svg"));
        List<PathInfo> results = extractPathsWithClassSt5(document );

        for (PathInfo info : results) {
            System.out.println("d: " + info.d);
            System.out.println("transform: " + info.transform);
            System.out.println("---");
        }
    }

    public static List<PathInfo> extractPathsWithClassSt5(Document doc) throws Exception {
//        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//        factory.setNamespaceAware(true); // 必须启用命名空间支持
//        DocumentBuilder builder = factory.newDocumentBuilder();
//        Document doc = builder.parse(new ByteArrayInputStream(svgXml.getBytes(StandardCharsets.UTF_8)));

        XPath xpath = XPathFactory.newInstance().newXPath();

        // 注册命名空间上下文（SVG 默认命名空间）
        xpath.setNamespaceContext(new javax.xml.namespace.NamespaceContext() {
            @Override
            public String getNamespaceURI(String prefix) {
                if ("svg".equals(prefix) || "".equals(prefix)) {
                    return "http://www.w3.org/2000/svg";
                }
                return null;
            }

            @Override
            public String getPrefix(String namespaceURI) {
                return null;
            }

            @Override
            public java.util.Iterator<String> getPrefixes(String namespaceURI) {
                return null;
            }
        });

        // 查找所有 class="st5" 的 path 元素
        String expression = "//svg:path[@class='st5']";
        NodeList nodeList = (NodeList) xpath.compile(expression).evaluate(doc, XPathConstants.NODESET);

        List<PathInfo> result = new ArrayList<>();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Element pathElement = (Element) nodeList.item(i);
            String d = pathElement.getAttribute("d");

            // 获取父节点（应为 <g>）
            Node parent = pathElement.getParentNode();
            String transform = "";
            if (parent instanceof Element) {
                transform = ((Element) parent).getAttribute("transform");
            }

            result.add(new PathInfo(d, transform));
        }

        return result;
    }

    static class PathInfo {
        String d;
        String transform;

        PathInfo(String d, String transform) {
            this.d = d;
            this.transform = transform;
        }
    }
}